package com.thuanpq60023.easyenglish.helper;

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import android.content.Context;
import android.database.sqlite.SQLiteConstraintException;
import android.util.Log;

import com.thuanpq60023.easyenglish.adapter.DatabaseAdapter;
import com.thuanpq60023.easyenglish.bus.UnitBus;
import com.thuanpq60023.easyenglish.bus.VocabularyBus;
import com.thuanpq60023.easyenglish.entity.UnitEntity;
import com.thuanpq60023.easyenglish.entity.VocabEntity;
import com.thuanpq60023.easyenglish.global.GlobalFunction;

public class ReadXLS {
	private Context ctx;
	private VocabularyBus mVocabularyBus;
	private UnitBus mUnitBus;
	public static final String XSLNAME = "easyenglish.xls";
	private String strWord;
	private String strMean;
	private String strType;
	private String strExample;
	private String strUnit;
	private String strCreatedDate;
	private int intRight;
	private int intWrong;

	public ReadXLS(Context context) {
		this.ctx = context;
		mVocabularyBus = new VocabularyBus(ctx);
		mUnitBus = new UnitBus(ctx);
	}

	public String[] getSheetName() {
		String[] sheetsname = null;
		File inputWorkbook = new File(DatabaseAdapter.DATABASE_FILE_PATH
				+ File.separator + XSLNAME);
		Workbook w;
		try {
			if (inputWorkbook.exists()) {
				Log.e("ThuanPQ", "easyenglish.xls exists");
				w = Workbook.getWorkbook(inputWorkbook);
				sheetsname = w.getSheetNames();
			}
		} catch (Exception e) {
			Log.e("ThuanPQ", "getSheet(): " + e.toString());
		}
		return sheetsname;
	}

	public Sheet[] getSheet() {
		Sheet[] sheets = null;
		File inputWorkbook = new File(DatabaseAdapter.DATABASE_FILE_PATH
				+ File.separator + XSLNAME);
		Workbook w;
		try {
			if (inputWorkbook.exists()) {
				Log.e("ThuanPQ", "easyenglish.xls exists");
				w = Workbook.getWorkbook(inputWorkbook);
				sheets = w.getSheets();
			}
		} catch (Exception e) {
			Log.e("ThuanPQ", "getSheet(): " + e.toString());
		}
		return sheets;
	}

	public boolean multiImport(boolean[] sheetNo) {
		boolean flag = true;
		File inputWorkbook = new File(DatabaseAdapter.DATABASE_FILE_PATH
				+ File.separator + XSLNAME);
		Workbook w;
		try {
			if (inputWorkbook.exists()) {
				Log.e("ThuanPQ", "easyenglish.xls exists");

				w = Workbook.getWorkbook(inputWorkbook);
				for (int i = 0; i < sheetNo.length; i++) {
					if (sheetNo[i]) {

						Sheet sheet = w.getSheet(i);
						try {
							for (int j = 1; j < sheet.getRows(); j++) {

								strWord = sheet.getCell(0, j).getContents()
										.trim();
								strMean = sheet.getCell(1, j).getContents()
										.trim();
								strType = sheet.getCell(2, j).getContents()
										.trim();
								strExample = sheet.getCell(3, j).getContents()
										.trim();
								strUnit = sheet.getCell(4, j).getContents()
										.trim();
								strCreatedDate = GlobalFunction
										.getCurrentDate();
								intRight = Integer.parseInt(sheet.getCell(5, j)
										.getContents().trim());
								intWrong = Integer.parseInt(sheet.getCell(6, j)
										.getContents().trim());

								if (!strWord.equalsIgnoreCase("")
										&& !strMean.equalsIgnoreCase("")
										&& !strType.equalsIgnoreCase("")) {
									if (!mUnitBus.checkUnit(strUnit))
										mUnitBus.insertUnit(new UnitEntity(
												strUnit, strCreatedDate));
									if (mVocabularyBus.getVocabByWordAndType(
											strWord, strType) == null)
										mVocabularyBus
												.insertVocab(new VocabEntity(
														strWord, strMean,
														strType, strExample,
														strUnit,
														strCreatedDate,
														intRight, intWrong));
									Log.e("ThuanPQ", "Add: " + strWord);
								}
							}
						} catch (NumberFormatException e) {
							Log.e("ThuanPQ", "ReadXLS(): " + e.toString());
						} catch (SQLiteConstraintException e) {
							flag = false;
							Log.e("ThuanPQ", "ReadXLS(): " + e.toString());
						}
					}
				}
			}
		} catch (IOException e) {
			Log.e("ThuanPQ", "ReadXLS(): " + e.toString());
		} catch (BiffException e) {
			Log.e("ThuanPQ", "ReadXLS(): " + e.toString());
		} finally {
			flag = false;
		}

		return flag;
	}
}